package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.IdCardUtil;
import com.sdy.healthcode.biz.dto.CarCheckInInfoDTO;
import com.sdy.healthcode.biz.dto.CarRecordDTO;
import com.sdy.healthcode.biz.dto.UserActionPost;
import com.sdy.healthcode.biz.model.*;
import com.sdy.healthcode.biz.mapper.VehicleRegistrationMapper;
import com.sdy.healthcode.biz.service.*;
import com.sdy.healthcode.biz.utils.ParamChecker;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cgj
 * @since 2020-03-16
 */
@Slf4j
@Service
public class VehicleRegistrationServiceImpl extends BaseServiceImpl<VehicleRegistration> implements VehicleRegistrationService {
    @Autowired
    private VehicleRegistrationMapper vehicleRegistrationMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private VehicleRegistrationService vehicleRegistrationService;
    @Autowired
    private BayonetManageService bayonetManageService;
    @Autowired
    private UserActionService userActionService;
    @Autowired
    private HsDepartmentService hsDepartmentService;

    @Override
    public Boolean createCarRecord(CarRecordDTO carRecordDTO) throws BizException {
        Assert.isBlank(carRecordDTO.getName(), "姓名不可为空!");
        Assert.isBlank(carRecordDTO.getVisitPeople(), "请正确填写拜访人员信息!");
        String idCard = IdCardUtil.checkIdCard(carRecordDTO.getCardId());
        Assert.isTrue(!"true".equals(idCard),"身份证格式错误");
        Assert.isTrue(!carRecordDTO.getPhone().matches(com.sdy.common.constant.Constants.REGEX_PHONE), "手机号格式错误!");
        Date date = new Date();
        // 补充用户信息
        User user = new User();
        user.setId(carRecordDTO.getUserId())
                .setName(carRecordDTO.getName())
                .setPhone(carRecordDTO.getPhone())
                .setCardId(carRecordDTO.getCardId())
                .setPeopleType(1)
                .setCarNum(carRecordDTO.getCarNum());
        boolean update = userService.updateById(user);
        Assert.notTrue(update, "更新用户信息时出错!");

        // 新增车辆登记数据
        String deptName = null;
        if (carRecordDTO.getVisitDeptId() != null) {
            HsDepartment byId = hsDepartmentService.getById(carRecordDTO.getVisitDeptId());
            deptName = byId.getName();
        }

        VehicleRegistration vehicleRegistration = new VehicleRegistration();
        vehicleRegistration
                .setUserId(carRecordDTO.getUserId())
                .setVisitDepartment(carRecordDTO.getVisitDeptId())
                .setVisitPeople(carRecordDTO.getVisitPeople())
                .setCreateTime(date)
                .setAction(carRecordDTO.getAction())
                .setCarNum(carRecordDTO.getCarNum())
                .setPeopleType(1)
                .setName(carRecordDTO.getName())
                .setCardId(carRecordDTO.getCardId())
                .setVisitDeptName(deptName)
                .setPhone(carRecordDTO.getPhone());
        boolean save = vehicleRegistrationService.save(vehicleRegistration);
        Integer id = vehicleRegistration.getId();
        Assert.notTrue(save, "新增车辆信息失败!");

        // 新增人员通行记录
        Integer action = null;
        if (carRecordDTO.getAction() == 1) {
            action = 2;
        } else if (carRecordDTO.getAction() == 2) {
            action = 1;
        }
        BayonetManage bayonetManage = bayonetManageService.lambdaQuery()
                .eq(BayonetManage::getBayonetToken, carRecordDTO.getTk())
                .last("limit 1")
                .one();
        UserAction userAction = new UserAction()
                .setUserId(carRecordDTO.getUserId())
                .setAction(carRecordDTO.getAction())
                .setRecordDate(DateUtil.getDate(date, "yyyyMMdd"))
                .setRecordTime(date)
                .setBayonetManageId(bayonetManage == null ? null : bayonetManage.getId())
                .setVehicleId(id)
                .setType(4)
                .setGoOut(action)
                .setStrategy(1)
                .setHealthStatus(1)
                .setRecorderId(carRecordDTO.getUserId());
        userActionService.save(userAction);
        return true;
    }

    @Override
    public List<CarCheckInInfoDTO> getCarCheckInInfo(Integer action, String name, String phone, String startTime, String endTime, String visitDepartment, Page page) {
        return vehicleRegistrationMapper.getCarCheckInInfo(action, name, phone, startTime, endTime, visitDepartment, page);
    }

    @Override
    public Integer saveRecord(UserActionPost userActionPost) throws BizException {
        VehicleRegistration vehicleRegistration = new VehicleRegistration()
                .setUserId(userActionPost.getUserId())
                .setAction(userActionPost.getAction())
                .setCarNum(userActionPost.getCarNum())
                .setCreateTime(new Date())
                .setPeopleType(2);
        boolean save = vehicleRegistrationService.save(vehicleRegistration);
        Assert.notTrue(save, "新增车辆登记数据出错!");
        Integer id = vehicleRegistration.getId();
        return id;
    }

}
